from scrapy.pipelines.images import ImagesPipeline
from scrapy.http import Request
from scrapy.exceptions import DropItem
from kxchina.spiders.orm import sessionMaker
import os,scrapy


class ImageDownloadPipLine(ImagesPipeline):
    def file_path(self,request):
        image_guid = request.url.split('/')[-1]
        return 'full/%s' % (image_guid)

    def get_media_requests(self, item, info):
        for image_url in item['images']:
            print 'use userdefined piplines'
            yield Request(image_url)

    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        #item['image_paths'] = image_paths
        self.opened_session = sessionMaker()
        print 'use userdefined piplines'
        self.opened_session.data_interface(item)
        print 'do data save after download'
        return item
        
